import { onCleanup } from 'solid-js'

export const uniqTimer = (
  set: (fn: () => void, time: number) => number,
  clear: (id: number) => void
) => {
  let id = 0
  const clear_ = () => clear(id)
  const set_ = (fn: () => void, time: number) => {
    clear_()
    id = set(fn, time)
  }
  onCleanup(clear_)
  return [set_, clear_] as const
}

export const assertNot = <T>(v: T, f: (v: T) => boolean) => {
  if (f(v)) throw ''
  return v
}
